package com.zhao.config;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * @program: mybatis-plus
 * @description: 乐观锁配置
 * @author: Stevezhao
 * @create: 2021-07-12 16:01
 */

@MapperScan("com.zhao.mapper")//扫描文件夹
@EnableTransactionManagement //事务管理
@Configuration //代表是配置类
public class MyBatisPlusConfig {
    //注册乐观锁插件
    //旧版
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    //分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    //逻辑删除
    @Bean
    public ISqlInjector sqlInjector(){
        return new LogicSqlInjector();
    }
    //性能分析插件
    @Bean
    @Profile({"dev","test"})//设置 dev ,test 环境
    public PerformanceInterceptor performanceInterceptor(){
       PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        //设置sql最大的时间，超过1ms不执行
       performanceInterceptor.setMaxTime(100);
        //格式化支持
       performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }

}
